package vista;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;

import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

import sistema.Sistema;
import vista.verificaciones.VerificarEntrada;

/**
* Clase que realiza el ALTA del Usuario RESIDENCIAL
*/
public class PanelUsuarioResidencial extends javax.swing.JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JTextField txtNombre;
	private JTextField txtDepto;
	private JLabel lblPiso;
	private JTextField txtPiso;
	private JLabel lblTituloUsuario;
	private JButton btnCancelar;
	private JButton btnAceptar;
	private JLabel lblCp;
	private JTextField txtCp;
	private JLabel lblDepto;
	private JTextField txtAltura;
	private JLabel lblAltura;
	private JLabel lblCalle;
	private JLabel lblProvincia;
	private JTextField txtCalle;
	private JTextField txtPcia;
	private JLabel lblApellido;
	private JLabel lblNombre;
	private JTextField txtApellido;
	
	protected JFrame frame;
	
	private Sistema sistema;
	
	//Comandos para los Action Listener
	
	private static String CMD_ACEPTAR="cmd_aceptar";
	private static String CMD_CANCELAR="cmd_cancelar";


	public PanelUsuarioResidencial(JFrame frame) {
		super();
		//Guardamos el frame que contiene al panel. Cuando aprete cancelar, 
		//se oculta la ventana
		this.frame = frame;
		initGUI();
		
		//INSTANCIA DE SISTEMA
		this.sistema= Sistema.getInstancia();
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(659, 300));
			{
				txtNombre = new JTextField();
				
			}
			{
				txtApellido = new JTextField();
			}
			{
				lblNombre = new JLabel();
				lblNombre.setText("Nombre: ");
			}
			{
				lblTituloUsuario = new JLabel();
				lblTituloUsuario.setText("USUARIO RESIDENCIAL");
			}
			{
				btnAceptar = new JButton();
				btnAceptar.setText("ACEPTAR");
				btnAceptar.addActionListener(this);
				btnAceptar.setActionCommand(CMD_ACEPTAR);
			
			}
			{
				btnCancelar = new JButton();
				btnCancelar.setText("CANCELAR");
				btnCancelar.addActionListener(this);
				btnCancelar.setActionCommand(CMD_CANCELAR);
			}
			{
				txtCp = new JTextField();
			}
			{
				lblCp = new JLabel();
				lblCp.setText("CP");
			}
			{
				txtDepto = new JTextField();
			}
			{
				lblDepto = new JLabel();
				lblDepto.setText("Depto");
			}
			{
				lblAltura = new JLabel();
				lblAltura.setText("Altura");
			}
			{
				txtAltura = new JTextField();
			}
			{
				txtPiso = new JTextField();
			}
			{
				lblPiso = new JLabel();
				lblPiso.setText("Piso");
			}
			{
				txtPcia = new JTextField();
			}
			{
				txtCalle = new JTextField();
				
			}
			{
				lblProvincia = new JLabel();
				lblProvincia.setText("Provincia");
			}
			{
				lblCalle = new JLabel();
				lblCalle.setText("Calle");
			}
			{
				lblApellido = new JLabel();
				lblApellido.setText("Apellido");
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(lblTituloUsuario, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
				.addGap(19)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(txtApellido, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
				    .addComponent(lblApellido, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
				    .addComponent(txtNombre, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
				    .addComponent(lblNombre, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE))
				.addGap(25)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(txtDepto, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(lblDepto, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(txtPiso, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(lblPiso, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(txtCalle, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(lblCalle, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(lblAltura, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(txtAltura, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGap(30)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(txtCp, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(lblCp, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(txtPcia, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
				    .addComponent(lblProvincia, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGap(29)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(btnAceptar, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(btnCancelar, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addContainerGap(74, 74));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(lblProvincia, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(lblCalle, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
				        .addGap(31))
				    .addComponent(lblNombre, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(txtPcia, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
				            .addComponent(txtCalle, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
				            .addComponent(txtNombre, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE))
				        .addGap(7)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addGap(0, 0, Short.MAX_VALUE)
				                .addGroup(thisLayout.createParallelGroup()
				                    .addComponent(lblCp, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
				                    .addComponent(lblAltura, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
				                .addGap(0, 48, GroupLayout.PREFERRED_SIZE)
				                .addComponent(btnCancelar, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
				                .addGap(122))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addGap(0, 30, Short.MAX_VALUE)
				                .addComponent(lblApellido, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				                .addComponent(txtApellido, GroupLayout.PREFERRED_SIZE, 221, GroupLayout.PREFERRED_SIZE)))
				        .addGap(50))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGap(119)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(btnAceptar, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
				                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, Short.MAX_VALUE)
				                .addGroup(thisLayout.createParallelGroup()
				                    .addComponent(txtCp, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				                    .addComponent(txtAltura, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE))
				                .addGap(12))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addGap(42)
				                .addComponent(lblTituloUsuario, 0, 139, Short.MAX_VALUE)))
				        .addComponent(lblPiso, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addComponent(txtPiso, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				        .addComponent(lblDepto, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addComponent(txtDepto, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)))
				.addContainerGap(28, 28));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//Get y Set de los datos ingresados
		public String getTxtNombre() {
			return this.txtNombre.getText();
		}

		public void setTxtNombre(String nombre) {
			this.txtNombre.setText(nombre);
		}

		public String getTxtDepto() {
			return this.txtDepto.getText();
		}

		public void setTxtDepto(String depto) {
			this.txtDepto.setText(depto);
		}

		public String getTxtPiso() {
			return this.txtPiso.getText();
		}

		public void setTxtPiso(String piso) {
			this.txtPiso.setText(piso);
		}



		public String getTxtCp() {
			return this.txtCp.getText();
		}

		public void setTxtCp(String txtCp) {
			this.txtCp.setText(txtCp);
		}

		public Integer getTxtAltura() {
			try {
				return Integer.parseInt(txtAltura.getText());

			} catch (NumberFormatException e) {
				e.printStackTrace();
				return -1;
			}
		}

		public void setTxtAltura(String txtAltura) {
			this.txtAltura.setText(txtAltura);
		}

		public String getTxtCalle() {
			return this.txtCalle.getText();
		}

		public void setTxtCalle(String txtCalle) {
			this.txtCalle.setText(txtCalle);
		}

		public String getTxtPcia() {
			return this.txtPcia.getText();
		}

		public void setTxtPcia(String txtPcia) {
			this.txtPcia.setText(txtPcia);
		}

		public String getTxtApellido() {
			return this.txtApellido.getText();
		}

		public void setTxtApellido(String txtApellido) {
			this.txtApellido.setText(txtApellido);
		}
		
		/**
		 * REALIZA LA LIMPIEZA DE LA PANTALLA
		 */
		private void realizarLimpiezaPantalla() {
			
			this.setTxtApellido("");
			this.setTxtNombre("");
			this.setTxtCalle("");
			this.setTxtAltura("");
			this.setTxtCp("");
			this.setTxtDepto("");
			this.setTxtPiso("");
			this.setTxtPcia("");
			
		}
		
		/**
		 * Verifica que todos los datos esten COMPLETOS
		 * @return False si falta ALGUN datos
		 * 		   True si estan TODOs los datos	
		 * 
		 */
			private boolean verificacion_entrada(){
			
				 String mensaje ="DEBE INGRESAR:";
				
				//Creo el objeto para la validacion
				VerificarEntrada verificar = new VerificarEntrada();
				
				if (!verificar.verificarVacios(this.txtApellido.getText())){
					JOptionPane.showMessageDialog(null, "APELLIDO", mensaje,JOptionPane.ERROR_MESSAGE );
		            return false;
				}
				
				if (!verificar.verificarVacios(this.txtNombre.getText())){
					JOptionPane.showMessageDialog(null, "NOMBRE", mensaje,JOptionPane.ERROR_MESSAGE );
					return false;
				}
				
				if (!verificar.verificarVacios(this.txtCalle.getText())){
					JOptionPane.showMessageDialog(null, "CALLE", mensaje,JOptionPane.ERROR_MESSAGE );
					return false;
				}
				
				if (!verificar.verificarVacios(this.txtAltura.getText())){
					JOptionPane.showMessageDialog(null, "ALTURA", mensaje,JOptionPane.ERROR_MESSAGE );
					return false;
				}
				
				
				if (!verificar.verificarVacios(this.txtCp.getText())){
					JOptionPane.showMessageDialog(null, "CP", mensaje,JOptionPane.ERROR_MESSAGE );
					return false;
				}
				
				if (!verificar.verificarVacios(this.txtPcia.getText())){
					JOptionPane.showMessageDialog(null, "PROVINCIA", mensaje,JOptionPane.ERROR_MESSAGE );
					return false;
				}
				
				return true;
			}	

	
			
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		//ACEPTAR
		if ( CMD_ACEPTAR.equals(arg0.getActionCommand()) ){
			//Verifico que todos los campos esten cargados
			if (verificacion_entrada()){
				//Deberia validar los datos
				//Doy el alta
				darAltaUsuario();	
				//Limpio los campos
			    this.realizarLimpiezaPantalla();
			}	
		}
		
		//CANCELAR
		else if ( CMD_CANCELAR.equals(arg0.getActionCommand()) ){
			this.frame.dispose();
		}
		
		
	}

	/**
	 * Da de alta el usuario
	 */
	private void darAltaUsuario() {
		
		
		this.sistema.agregarResidenc(getTxtCalle(), Integer.valueOf(getTxtAltura()), this.getTxtPiso(), 
									 this.getTxtDepto(), this.getTxtCp(), this.getTxtPcia(), 
									 this.getTxtNombre(), this.getTxtApellido());
		
		System.out.println(getTxtAltura());
	}

}
